package com.kyzh.core.current.WealImpl1

import com.gushenge.atools.util.ADate
import com.gushenge.core.base.Base
import com.gushenge.core.beans.Code
import com.gushenge.core.dao.MMKVConsts.sub
import com.gushenge.core.dao.MMKVConsts.uid
import com.kyzh.core.current.AnswerBean
import com.kyzh.core.current.RecoverBean
import com.kyzh.core.current.SubOption
import com.kyzh.core.utils.md5
import com.gushenge.core.toast
import com.rxlife.coroutine.RxLifeScope
import rxhttp.awaitResult
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

/**
 *
 * @Author:         yu
 * @CreateDate:     2021/8/5 15:44
 */
object WealImpl1 {
   val ANSWER ="${Base.BASEURLV3}?ct=app&ac=answer"
    val ANSWERSUBMIT= "${Base.BASEURLV3}?ct=app&ac=answer_submit"
    /*小号列表*/
    val SMALL_ACCOUNT_MANAGER = "${Base.BASEURLV3}?ct=app&ac=small_search"

    /*回收列表*/
    val RECOVER = "${Base.BASEURLV3}?ct=app&ac=recover"

    /*提交小号回收*/
    val SUBRECOVER = "${Base.BASEURLV3}?ct=app&ac=recover_add"

    /*回收记录*/
    val RECOVERORDER = "${Base.BASEURLV3}?ct=app&ac=recover_order"
    /*撤销*/
    val CHEXIAO = "${Base.BASEURLV3}?ct=app&ac=recover_chexiao"
    /**
     * 获取列表
     */
     fun answer(listener: AnswerBean?.() -> Unit) {
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(ANSWER)
                .add("uid",uid)
                .add("member_id",sub)
                .add("t",time)
                .add("sign",sign)
                .toClass<AnswerBean>()
                .await()
            if(result.code==1)
                listener(result)
            else
                "每日答题获取失败，请重试".toast()
        }
    }

    /**
     * 提交答案
     */
     fun answerSubmit(option: String,id: String,listener: SubOption?.() -> Unit) {
        val sign = md5("${uid}${id}${option}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(ANSWERSUBMIT)
                .add("uid",uid)
                .add("member_id",sub)
                .add("id",id)
                .add("option",option)
                .add("sign",sign)
                .toClass<Code<SubOption>>()
                .awaitResult {
                    if(it.code == 1 ){
                        listener(it.data)
                    }else {
                        it. message.toast()
                    }
                }.onFailure {
                    "提交失败，请重试".toast()
                }


        }
//        HttpQuery().answerSubmit(GlobalConsts.ANSWERSUBMIT,uid,SpConsts.sub,id,option,sign).enqueue(object : Callback<Code<SubOption>> {
//            override fun onResponse(call: Call<Code<SubOption>>, response: Response<Code<SubOption>>) {
//                response.body()?.apply {
//                    if (code == 1) {
//                        listener.invoke(this.data)
//                    } else {
//                        message.toast()
//                    }
//                }
//            }
//
//            override fun onFailure(call: Call<Code<SubOption>>, t: Throwable) {
//                "提交失败，请重试".toast()
//            }
//
//        })
    }

    /**
     * 获取回收列表
     */
    fun getRecover(listener: RecoverBean?.()->Unit){
        val time = ADate.getStampAs13()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(RECOVER)
                .add("uid", uid)
                .add("member_id", sub)
                .add("t", time)
                .add("sign", sign)
                .toClass<RecoverBean>()
                .awaitResult {
                    it?.listener()
                    if (it.code != "1")
                        if(it.message != "没有了~！"){
                            it.message.toast()
                        }
                }.onFailure {
                    listener(null)
                }


        }
//        HttpQuery().recover(GlobalConsts.RECOVER,uid,SpConsts.sub, time.toString(),sign).enqueue(object : Callback<RecoverBean> {
//            override fun onResponse(call: Call<RecoverBean>, response: Response<RecoverBean>) {
//                response.body()?.apply {
//                    listener.invoke(this)
//                    if (code != "1") {

//                        message.toast()
//                    }
//                }
//            }
//            override fun onFailure(call: Call<RecoverBean>, t: Throwable) {
//                listener.invoke(null)
//                "请求失败，请重试".toast()
//            }
//        })
    }

    /**
     * 提交小号回收
     */
    fun recoverSub(small_uid:String,listener:Boolean.()->Unit){
        if(small_uid.isNullOrEmpty()){
            "错误，请联系客服".toast()
            return
        }
        val sign = md5("${uid}${small_uid}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(SUBRECOVER)
                .add("uid", uid)
                .add("member_id", sub)
                .add("small_uid", small_uid)
                .add("sign", sign)
                .toClass<Code<String>>()
                .awaitResult {
                    if (it.code == 1)
                        listener(true)
                    else
                        listener(false)
                }.onFailure {
                    listener.invoke(false)
                "提交失败，请重试".toast()
                }


        }
//        HttpQuery().recoverSub(GlobalConsts.SUBRECOVER,uid,SpConsts.sub, small_uid,sign).enqueue(object : Callback<Code<String>> {
//            override fun onResponse(call: Call<Code<String>>, response: Response<Code<String>>) {
//                Log.e("TAG", "onResponse: ${response.body()?.data}", )
//                response.body()?.apply {
//                    message.toast()
//                    if (code == 1) {
//                        listener.invoke(true)
//                    } else {
//                        listener.invoke(false)
//                    }
//                }
//            }
//            override fun onFailure(call: Call<Code<String>>, t: Throwable) {
//                listener.invoke(false)
//                "提交失败，请重试".toast()
//            }
//        })
    }

    fun recoverOrder(p:Int,listener: RecoverBean?.()->Unit){
        val time = ADate.getStampAs13().toString()
        val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(RECOVERORDER)
                .add("uid", uid)
                .add("member_id", sub)
                .add("t", time)
                .add("p", p)
                .add("sign", sign)
                .toClass<RecoverBean>()
                .awaitResult {
                    if (it.code == "1")
                        listener(it)
                    else
                        it. message.toast()
                }.onFailure {
                    "查询失败，请重试".toast()
                }


        }
//        HttpQuery().recoverOrder(GlobalConsts.RECOVERORDER,uid,SpConsts.sub, time,p,sign).enqueue(object : Callback<RecoverBean> {
//            override fun onResponse(call: Call<RecoverBean>, response: Response<RecoverBean>) {
//                Log.e("TAG", "onResponse: ${response.body()?.data}", )
//                response.body()?.apply {
////                    message.toast()
//                    if (code == "1") {
//                        listener(this)
//                    } else {
//                        message.toast()
//                    }
//                }
//            }
//            override fun onFailure(call: Call<RecoverBean>, t: Throwable) {
//                "查询失败，请重试".toast()
//            }
//        })
    }
    fun chexiao (id:String,content:String, listener:Boolean.()->Unit){
        if(content.isNullOrEmpty()){
            "请填写撤销原因".toast()
            listener(false)
            return
        }
        val sign = md5("${uid}${id}fsd213ewdsadqwe2121213edsad")
        RxLifeScope().launch {
            val result = RxHttp.get(CHEXIAO)
                .add("uid", uid)
                .add("member_id", sub)
                .add("id", id)
                .add("content", content)
                .add("sign", sign)
                .toClass<Code<String>>()
                .awaitResult {
                    it.message.toast()
                    if (it.code == 1)
                        listener(true)
                    else
                        listener(false)
                }.onFailure {
                    "提交失败，请重试".toast()
               listener(false)
                }


        }
//        HttpQuery().chexiao(GlobalConsts.CHEXIAO,uid,SpConsts.sub,id,content,sign).enqueue(object : Callback<Code<String>> {
//            override fun onResponse(call: Call<Code<String>>, response: Response<Code<String>>) {
//                response.body()?.apply {
//                    message.toast()
//                    if (code == 1) {
//                        listener(true)
//                    } else {
//                        listener(false)
//                    }
//                }
//            }
//
//            override fun onFailure(call: Call<Code<String>>, t: Throwable) {
//                "提交失败，请重试".toast()
//                listener(false)
//            }
//
//        })
    }
}